package com.hdax.controller;


import com.hdax.entity.Car;
import com.hdax.service.CarService;
import com.hdax.vo.CaridModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarService carService;

    @PostMapping("/findCarAll")
    public List<Car> findCarAll() {
        return carService.list();
    }

    @GetMapping("list")
    public ModelAndView list() {
        return new ModelAndView("car/list");
    }


    @PostMapping("/findCarById")
    public Car findCarById(Integer id){
        return carService.getById(id);
    }


    @RequestMapping(path = "/index")
    public Map<String, Object> index(Car series, @RequestParam("page") int page,
                                     @RequestParam("limit") int limit) {

        Map<String, Object> map = carService.listByPage(page, limit, series);

        return map;
    }

    @GetMapping(path = "/add")
    public ModelAndView toAdd() {
        return new ModelAndView("/car/add");
    }

    @PostMapping(path = "/add")
    public Map<String, Object> toAdd(Car car) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean flag = carService.save(car);
        map.put("msg", flag);
        return map;
    }

    @GetMapping(path = "/update/{id}")
    public ModelAndView toUpdate(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("car/update");
        Car car = carService.getById(id);
        modelAndView.addObject("car", car);
        return modelAndView;
    }

    @PostMapping(path = "/update")
    public Map<String, Object> toUpdate(Car car) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean flag = carService.updateById(car);
        map.put("msg", flag);
        return map;
    }

    @PostMapping(path = "/del")
    public Map<String, Object> del(CaridModel caridModel) {
        Map<String, Object> map = new HashMap<String, Object>();
        carService.btnDel(caridModel.getIds());
        map.put("msg", "success");
        return map;
    }

    @PostMapping(path = "delete/{id}")
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean flag = carService.removeById(id);
        map.put("msg", flag);
        return map;
    }
}

